﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PacManPP.Objects;
using System.Xml;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace PacManPP.Managers
{
    class BoardManager
    {  
        protected String name;
        protected String boardName;

        protected ContentManager content;
        protected SpriteBatch spriteBatch;
        protected GraphicsDevice graphics;

        protected List<Wall> walls = new List<Wall>();

        public List<Wall> Walls
        {
            get { return walls; }
            set { walls = value; }
        }

        public BoardManager(String name)
        {
            this.name = name;           
        }

        public ContentManager Content
        {
            get { return content; }
            set { content = value; }
        }

        public SpriteBatch SpriteBatch
        {
            get { return spriteBatch; }
            set { spriteBatch = value; }
        }

        public GraphicsDevice Graphics
        {
            get { return graphics; }
            set { graphics = value; }
        }


        private void addWall(Wall wallObject)
        {
            walls.Add(wallObject);
        }

        public void readBoard() {

            XmlDocument document = new XmlDocument();
            document.Load(name);

            boardName = document.DocumentElement.GetAttribute("name");
            XmlNodeList hWalls = document.GetElementsByTagName("Walls");           
            XmlNode wall;

            if (hWalls.Count > 0)
            {
                XmlNode wallList = hWalls.Item(0);

                for (int i = 0; i < wallList.ChildNodes.Count; i++)
                {
                    wall = wallList.ChildNodes[i];

                    string xString = wall.Attributes["x"].Value;
                    int x = Int32.Parse(xString);

                    string yString = wall.Attributes["y"].Value;
                    int y = Int32.Parse(yString);                  

                    string x1String = wall.Attributes["x1"].Value;
                    int x1 = Int32.Parse(x1String);

                    string y1String = wall.Attributes["y1"].Value;
                    int y1 = Int32.Parse(y1String);

                    //System.Console.WriteLine(x + " " + y);

                    Wall wallObject = new Wall(x, y , x1 ,y1);                                  
                    this.addWall(wallObject);

                    Wall wallObject1 = new Wall(30 - x1, y  , 30 -x , y1);                    
                    this.addWall(wallObject1);

                    Wall wallObject2 = new Wall(30-x1, 24-y1, 30-x,24- y);                          
                    this.addWall(wallObject2);

                    Wall wallObject3 = new Wall(x,24-y1,x1,24-y);                                  
                    this.addWall(wallObject3);               
     
                }
            }           
        }
        /*
        static public Boolean checkCollisionWithWall(Vector2 pacManPosition) {

            int radius = 8;
            Rectangle pacManRec = new Rectangle((int)pacManPosition.X + (GameManager.scale / 2) - radius,
                                             (int)pacManPosition.Y + (GameManager.scale / 2) - radius,
                                             2 * radius,
                                             2 * radius);

           


           
                Wall tmp = (Wall)anotherObject;
                float distance = Vector2.Distance(tmp.positionBegin, tmp.positionEnd);

                int height = (int)tmp.positionBegin.Y - (int)tmp.positionEnd.Y;
                int width = (int)tmp.positionBegin.X - (int)tmp.positionEnd.X;

                Rectangle wallRect = new Rectangle((int)tmp.positionBegin.X, (int)tmp.positionBegin.Y, width, height);


        }   */  
        static public Boolean checkCollisionWithMonster(Vector2 pacManPosition) 
        {
            foreach (var item in GameManager.monsters)
            {
                int radius = 8;
                Rectangle pacManRec = new Rectangle((int)pacManPosition.X + (GameManager.scale / 2) - radius,
                                              (int)pacManPosition.Y + (GameManager.scale / 2) - radius,
                                              2 * radius,
                                              2 * radius);

                Rectangle monsterRec = new Rectangle((int)item.Position.X, (int)item.Position.Y, 2 * radius, 2 * radius);

                if (pacManRec.Intersects(monsterRec))
                {
                    GameManager.lives--;
                    GameManager.pacMan.Position = new Vector2(GameManager.boardBegin.X, GameManager.boardBegin.Y);
                    GameManager.pacMan.poistion.X = GameManager.pacMan.poistion.X + GameManager.scale / 2 - 2;
                    GameManager.pacMan.poistion.Y = GameManager.pacMan.poistion.Y + GameManager.scale / 2 + 1;
                    foreach (var item1 in  GameManager.dots)
                    {
                        item1.killed = false;
                        item1.eat_by_pacman = false;
                       
                    }
                    GameManager.points = 0;
                    SoundManager.DeadSound();
                    return true;

                }
                    
            }
            return false;    
        }

        static public Boolean checkCollisionWithDot(Vector2 pacManPosition)
        {
            foreach (var item in GameManager.dots)
            {
                int radius = 15;
                Rectangle pacManRec = new Rectangle((int)pacManPosition.X + (GameManager.scale / 2) - radius,
                                              (int)pacManPosition.Y + (GameManager.scale / 2) - radius,
                                               radius,
                                              radius);

                Rectangle dot = new Rectangle((int)item.Position.X, (int)item.Position.Y,  12,  12);

                if (pacManRec.Intersects(dot))
                {
                    item.eat_by_pacman = true;
                    
                    //item.UnloadContent();
                   
                }
                                
            }
            return false;
        }

        static public Boolean checkCollisionWithWall(Vector2 pacManPosition)
        {
            foreach (var item in GameManager.walls)
            {
                int radius = 8;
                Rectangle pacManRec = new Rectangle((int)pacManPosition.X ,
                                              (int)pacManPosition.Y,
                                              10,
                                              10 );
                
                Wall wall = (Wall) item;
                Rectangle wallRec;
                if (wall.positionBegin.X == wall.positionEnd.X)
                {

                    wallRec = new Rectangle((int)wall.positionBegin.X, (int)wall.positionBegin.Y, 5, ((int)wall.positionEnd.Y)-(int)wall.positionBegin.Y);

                }
                else
                {
                    wallRec = new Rectangle((int)wall.positionBegin.X, (int)wall.positionBegin.Y, ((int)wall.positionEnd.X-(int)wall.positionBegin.X), 5);

                }


                if (pacManRec.Intersects(wallRec))
                {
                    //Console.WriteLine("Kolizaja z " + wallRec + " a pacman jest na " + pacManRec);
                    //wall.kolor = Color.Red;
                    return true;

                }

            }
            return false;
        }



        static public Boolean mosterWithWall(Vector2 mosterPostion)
        {
            foreach (var item in GameManager.walls)
            {
               
                Rectangle mosterRec = new Rectangle((int)mosterPostion.X,
                                              (int)mosterPostion.Y,
                                              10,
                                              10);

                Wall wall = (Wall)item;
                Rectangle wallRec;
                if (wall.positionBegin.X == wall.positionEnd.X)
                {

                    wallRec = new Rectangle((int)wall.positionBegin.X, (int)wall.positionBegin.Y, 5, ((int)wall.positionEnd.Y) - (int)wall.positionBegin.Y);

                }
                else
                {
                    wallRec = new Rectangle((int)wall.positionBegin.X, (int)wall.positionBegin.Y, ((int)wall.positionEnd.X - (int)wall.positionBegin.X), 5);

                }


                if (mosterRec.Intersects(wallRec))
                {                   
                    return true;
                }

            }
            return false;
        }

        static public Boolean mosterWithPacMan()
        {
            foreach (var item in GameManager.monsters)
            {
                int radius = 8;
                Rectangle pacManRec = new Rectangle((int)GameManager.pacMan.Position.X + (GameManager.scale / 2) - radius,
                                              (int)GameManager.pacMan.Position.Y + (GameManager.scale / 2) - radius,
                                              2 * radius,
                                              2 * radius);

                Rectangle monsterRec = new Rectangle((int)item.Position.X, (int)item.Position.Y, 2 * radius, 2 * radius);

                if (pacManRec.Intersects(monsterRec))
                {
                    GameManager.lives--;
                    GameManager.pacMan.Position = new Vector2(GameManager.boardBegin.X, GameManager.boardBegin.Y);
                    GameManager.pacMan.poistion.X = GameManager.pacMan.poistion.X + GameManager.scale / 2 - 2;
                    GameManager.pacMan.poistion.Y = GameManager.pacMan.poistion.Y + GameManager.scale / 2 + 1;
                    foreach (var item1 in GameManager.dots)
                    {
                        item1.killed = false;
                        item1.eat_by_pacman = false;

                    }
                    GameManager.points = 0;
                    SoundManager.DeadSound();
                    return true;

                }

            }
            return false;
        }

    }
}
